-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Class expressions #3568
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class expressions #3568
Conversation
if (node) { | ||
return node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression; | ||
} | ||
return node && (node.kind === SyntaxKind.ClassDeclaration || node.kind === SyntaxKind.ClassExpression); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make this !!node
so this never returns undefined
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to, undefined
is just as good.
👍 |
Should typeof operator works with class expressions?
|
@xLama It works, but var B = class {}
function foo(a:typeof B):void{}
foo(B); // Ok |
@ahejlsberg
|
@xLama Exactly. |
This PR adds type checking for class expressions, finishing the work started in #2567.
In a class expression, the class name is optional and, if specified, is only in scope in the class expression itself. This is similar to the optional name of a function expression. It is not possible to refer to the class instance type of a class expression outside the class expression, but the type can of course be matched structurally.
Class declarations and class expressions differ as follows:
class C { }
introduces an instance type namedC
and a constructor function namedC
with the typenew () => C
.class C { }
produces a constructor function value of typenew () => C
and introduces an instance type namedC
that is in scope only within the body of the class expression.class { }
produces a constructor function value of typenew () => { ... }
where{ ... }
is the anonymous instance type of the class expression.An example:
Fixes #497.